﻿/**
    xie yun define the object to hold all data source in application
	系统只提供这样一个实例
*
*/

ePOS.common.util.GuiFactory = function(){
	function __getGridPanelCfgFromDS(ds){
        var cols = [];
        with(ds.dt){
            for(var key  in fields){
                if(!(fields[key ].visible)) continue;
                cols.push({
                    header:fields[key ].desc,
                    dataIndex:fields[key ].name 
                });
            }
        };
        var def_cfg = {
            columns:cols,
            store:ds.getStore()
        };
        return def_cfg;
    };
    function __getStdEditFormCfgFromDS(ds,data){
        var items = [];
        with(ds.dt){
            for (var key in fields){
                if(!(fields[key].editable)) continue;
                var item = {
                    xtype:fields[key].xtype,
                    fieldLabel:fields[key].desc,
                    name:fields[key].name
                };
                if (ds != undefined){
                	item.value = data[item.name];
                	//console.log(rec.data[key],data,'with the key',key);
                }
                items.push(item);
            }
        }
        return {items:items};
    };
    DSGridPanel = Ext.extend(Ext.grid.GridPanel,{
		initComponent:function(){
			if(this.dataset){
			//自动配置Column
				var gridCfg = __getGridPanelCfgFromDS(this.dataset);
				Ext.apply(this,gridCfg);
			}
			var tbs = [];
			if(this.mode == "update"){
				this.addEvents('addRecord','deleteRecord','updateRecord','search','selected');
				tbs = [{
	                text:'增加',
	                handler: function(){this.addupdateRecord();},
	                scope:this
	            },{
	                text:'修改',
	                handler: function(){
	                	if(this.curRec){
	                		this.addupdateRecord(this.curRec);
	                	}else{
	                		Ext.Msg.alert('error','please select the correct record to delete');
	                	}
	                },
	                scope:this
	            },{
	                text:'删除',
	                handler: function(){
	                	if(this.curRec){
	                		this.deleteRecord(this.curRec);
	                	}else{
	                		Ext.Msg.alert('error','please select the correct record to delete');
	                	}
	                },
	                scope:this
	            }];
	        };
	        tbs.push({
	                text:'搜索',
	                xtype:'tbtext',
	                handler:function(){this.fireEvent('search');},
	                scope:this
	            })
	        tbs.push({
	                xtype:'textfield',
	                id:'inv_search_text'
	            });
	        this.tbar = tbs;
	        this.sm = new Ext.grid.RowSelectionModel();
	        this.sm.addListener('rowselect',
	            function(sm,rowIndex,rec){
	                this.curRec = rec;
	                this.fireEvent('selected',this.curRow);
	            },
	            this);
	        ePOS.common.guifactory.DSGridPanel.superclass.initComponent.call(this);
		},
		addupdateRecord:function(rec){
			var win = new ePOS.common.guifactory.DSWindow({
				dataset:this.dataset,
				rec:rec,
				autoHeight: true,
            	modal:true
			});
        	win.show();
		},
		deleteRecord:function(rec){
			var ds = this.dataset;
			Ext.Msg.show({
	            title:'确认删除?',
	            msg: 'You are delete selected record',
	            buttons: Ext.Msg.YESNO,
	            fn: function(btnid,text,opt){
	                if(btnid == 'yes'){
	                    ds.deleteRecord(rec);
	                }
	            },
	            animEl: 'elId',
	            icon: Ext.MessageBox.QUESTION
	        });
		}
	});
	DSWindow = Ext.extend(Ext.Window,{
		initComponent:function(){
			if(this.dataset){
				var frm = new ePOS.common.guifactory.DSFormPanel({
					dataset:this.dataset,
					rec:this.rec
				});
				if(this.rec){
					this.mode = 'update';
				}else
				{
					this.mode = 'add';
				}
				this.items = this.items ? this.items:[]
				this.items.push(frm);
				this.buttons = this.buttons ? this.buttons:[];
				this.buttons.push({
                    text:'保存',
                    handler: function(){this.submitChange(frm.getResult());},
					scope:this
                });
				this.buttons.push({
					text:'取消',
                    handler:function(){this.destroy();},
                    scope:this
				});
			}
			ePOS.common.guifactory.DSWindow.superclass.initComponent.call(this);
		}
		,submitChange: function(res){
			if(this.mode == 'update'){
            	this.dataset.update(this.rec,res,{
            		success:function(){
            				this.destroy();
            			},
            		failure:function(){
            				Ext.Msg.alert('错误','提交修改错误');
            			},
            		scope:this
            		}
            	);	//orignal value and new value
            }else{
            	this.dataset.add(res,{
            		success:function(){
            				this.destroy();
            			},
            		failure:function(msg){
            				Ext.Msg.alert('错误','提交新纪录错误');
            			},
            		scope:this
            		}
            	);
            }
		}
	});
	DSFormPanel = Ext.extend(Ext.form.FormPanel,{
    	initComponent:function(){
    		if(this.dataset){
			//自动配置Column
    			if(this.rec){
    				this.mode = 'update';
    			}else{
    				this.mode = 'add';
    				this.rec = this.dataset.createBlankRecord();
    			}
				var formCfg = __getStdEditFormCfgFromDS(this.dataset,this.rec.data);
				Ext.apply(this, Ext.apply(this.initialConfig, formCfg));
			}
			ePOS.common.guifactory.DSFormPanel.superclass.initComponent.call(this);
    	},
    	getResult:function(){
            return this.getForm().getValues();
    	}
    });
    DSWindow = Ext.extend(Ext.Window,{
		initComponent:function(){
			if(this.dataset){
				var frm = new ePOS.common.guifactory.DSFormPanel({
					dataset:this.dataset,
					rec:this.rec
				});
				if(this.rec){
					this.mode = 'update';
				}else
				{
					this.mode = 'add';
				}
				this.items = this.items ? this.items:[]
				this.items.push(frm);
				this.buttons = this.buttons ? this.buttons:[];
				this.buttons.push({
                    text:'保存',
                    handler: function(){this.submitChange(frm.getResult());},
					scope:this
                });
				this.buttons.push({
					text:'取消',
                    handler:function(){this.destroy();},
                    scope:this
				});
			}
			ePOS.common.guifactory.DSWindow.superclass.initComponent.call(this);
		}
		,submitChange: function(res){
			if(this.mode == 'update'){
            	this.dataset.update(this.rec,res,{
            		success:function(){
            				this.destroy();
            			},
            		failure:function(){
            				Ext.Msg.alert('错误','提交修改错误');
            			},
            		scope:this
            		}
            	);	//orignal value and new value
            }else{
            	this.dataset.add(res,{
            		success:function(){
            				this.destroy();
            			},
            		failure:function(msg){
            				Ext.Msg.alert('错误','提交新纪录错误');
            			},
            		scope:this
            		}
            	);
            }
		}
	});
	DSFormPanel = Ext.extend(Ext.form.FormPanel,{
    	initComponent:function(){
    		if(this.dataset){
			//自动配置Column
    			if(this.rec){
    				this.mode = 'update';
    			}else{
    				this.mode = 'add';
    				this.rec = this.dataset.createBlankRecord();
    			}
				var formCfg = __getStdEditFormCfgFromDS(this.dataset,this.rec.data);
				Ext.apply(this, Ext.apply(this.initialConfig, formCfg));
			}
			ePOS.common.guifactory.DSFormPanel.superclass.initComponent.call(this);
    	},
    	getResult:function(){
            return this.getForm().getValues();
    	}
    });
	Ext.apply(ePOS.common.util.GuiFactory.prototype,{
		getInvListPanel:function(){
			return new DSGridPanel;
		}
	})
};


	
        
   

